{% extends "../page.html" %}

{% block content %}
<form type="yunj" id="{{ data.builderId }}"></form>
<script type="text/javascript">
    layui.use(["jquery", 'yunj'], function () {
        let win = window;
        let doc = document;
        let $ = layui.jquery;

        class FormHandler{
            constructor(args) {
                this.args = args;
                this.formBuilderId = args.formBuilderId;
                this.isEdit = args.isEdit;
                this.isAddChild = args.isAddChild;
                this.isSystem = args.isSystem;
                this.parentDetailApi = args.parentDetailApi;
                this.parentAuthDetailPromise = null;
                this.init();
            }

            init() {
                let that = this;
                that.setEventBind();
            }

            // 处理类型相关字段的显示隐藏
            handleTypeFieldShowHide(form) {
                let that = this;
                let typeObj = form.getFieldObj('type');
                let iconObj = form.getFieldObj('icon');
                if ([11, 22].indexOf(typeObj.getValue()) !== -1) {
                    that.setFieldShowHide(iconObj,true);
                } else {
                    that.setFieldShowHide(iconObj,false);
                }
            }

            // 处理请求类型相关字段的显示隐藏
            handleRequestModeFieldShowHide(form) {
                let that = this;
                let requestModeObj = form.getFieldObj('request_mode');
                let pageOpenObj = form.getFieldObj('page_open');
                let requestUrlObj = form.getFieldObj('request_url');
                let requestMethodObj = form.getFieldObj('request_method');
                let requestRequireParamsObj = form.getFieldObj('request_require_params');
                let createChildAuthsObj = form.getFieldObj('create_child_auths');
                switch (requestModeObj.getValue()) {
                    case 0:
                        that.setFieldShowHide(pageOpenObj,false);
                        that.setFieldShowHide(requestUrlObj,false);
                        that.setFieldShowHide(requestMethodObj,false);
                        that.setFieldShowHide(requestRequireParamsObj,false);
                        that.setFieldShowHide(createChildAuthsObj,false);
                        break;
                    case 11:
                        that.setFieldShowHide(pageOpenObj,true);
                        that.setFieldShowHide(requestUrlObj,true);
                        that.setFieldShowHide(requestMethodObj,true);
                        that.setFieldShowHide(requestRequireParamsObj,true);
                        that.setFieldShowHide(createChildAuthsObj,true);
                        break;
                    case 22:
                        that.setFieldShowHide(pageOpenObj,true);
                        that.setFieldShowHide(requestUrlObj,true);
                        that.setFieldShowHide(requestMethodObj,false);
                        that.setFieldShowHide(requestRequireParamsObj,false);
                        that.setFieldShowHide(createChildAuthsObj,false);
                        break;
                }
            }

            // 设置字段的显示/隐藏
            setFieldShowHide(fieldObj,isShow){
                if (!fieldObj) {
                    return;
                }
                if(isShow){
                    fieldObj.boxEl.parent("div").show();
                }else{
                    fieldObj.boxEl.parent("div").hide();
                }
            }

            setEventBind(){
                let that = this

                // 绑定渲染完成后的事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_render_done`, function (e, form) {
                    that.handleTypeFieldShowHide(form);
                    that.handleRequestModeFieldShowHide(form);
                });
                // 监听类型切换事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_type_change`, function (e, field) {
                    let form = yunj.formObj(field.formId);
                    that.handleTypeFieldShowHide(form);
                });
                // 监听请求方式切换事件
                $(doc).bind(`yunj_form_${that.formBuilderId}_request_mode_change`, function (e, field) {
                    let form = yunj.formObj(field.formId);
                    that.handleRequestModeFieldShowHide(form);
                });
            }

        }

        $(doc).ready(function () {
            new FormHandler({
                formBuilderId: '{{ data.builderId }}',
                isEdit: {% if data.isEdit %}true{% else %}false{% endif %},
                isAddChild: {% if data.isAddChild %}true{% else %}false{% endif %},
                isSystem: {% if data.isSystem %}true{% else %}false{% endif %},
                parentDetailApi: '{{ data.parentDetailApi }}',
            });
        });
    });
</script>
{% endblock %}